from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import QFileDialog
from qfluentwidgets import PushSettingCard, FluentIconBase, qconfig

class ConfigurablePushSettingCard(PushSettingCard):
    """ 可配置的按钮设置卡片 """
    
    valueChanged = Signal(str)

    def __init__(self, configItem, text: str, icon: FluentIconBase, title: str, content=None, parent=None):
        """
        Parameters
        ----------
        configItem: ConfigItem
            配置项
        text: str 
            按钮文本
        icon: FluentIconBase | str | QIcon
            图标
        title: str
            标题
        content: str
            内容描述
        parent: QWidget
            父级部件
        """
        super().__init__(text, icon, title, content, parent)
        self.configItem = configItem

        if configItem:
            self.setValue(qconfig.get(configItem))
            configItem.valueChanged.connect(self.setValue)

        self.clicked.connect(self.__onClicked)

    def __onClicked(self):
        """ 按钮点击信号槽 """
        folder = QFileDialog.getExistingDirectory(
            self,
            "选择保存路径",
            self.configItem.value
        )
        if not folder or self.configItem.value == folder:
            return

        self.setValue(folder)
        self.valueChanged.emit(folder)

    def setValue(self, value: str):
        """ 设置值并保存配置 """
        if self.configItem:
            qconfig.set(self.configItem, value)
        self.setContent(value) 